package cn.tedu.truetaste.controller;

import cn.tedu.truetaste.pojo.dto.ShopAddNewDTO;
import cn.tedu.truetaste.pojo.dto.ShopUpdateDTO;
import cn.tedu.truetaste.pojo.vo.ShopStandardVO;
import cn.tedu.truetaste.restful.JsonPage;
import cn.tedu.truetaste.service.IShopService;
import cn.tedu.truetaste.util.WebConsts;
import cn.tedu.truetaste.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/shop")
@Api(tags ="购物车管理模块")
@Slf4j
public class ShopController {

    @Autowired
    private IShopService shopService;

    @PostMapping("/add")
    @ApiOperation("新增sku信息到购物车")
    //@PreAuthorize("hasAuthority('ROLE_user')")
    public JsonResult addShop(@Validated ShopAddNewDTO shopAddNewDTO){
        shopService.addShop(shopAddNewDTO);
        return JsonResult.ok("新增sku到购物车完成!");
    }

    @ApiOperation("根据用户id查询购物车详情")
    //@ApiOperationSupport(order = 400)
    @GetMapping("/{userId:[0-9]+}")
    public JsonResult<ShopStandardVO> getStandardByUserId(@PathVariable Long userId) {
        log.debug("开始处理【根据用户id查询购物车详情】的请求，参数：{}", userId);
        ShopStandardVO shopStandardVO = shopService.getStandardByUserId(userId);
        return JsonResult.ok(shopStandardVO);
    }

    @GetMapping("/list")
    @ApiOperation("根据用户Id分页查询购物车sku列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",example = "1"),
            @ApiImplicitParam(value = "每页条数",name="pageSize",example = "2")
    })

    //@PreAuthorize("hasAuthority('ROLE_user')")
    public JsonResult<JsonPage<ShopStandardVO>> listShopByPage(
            // 控制器方法参数可以设置默认值,在调用时如果这个参数没有值,会使用默认值
            // @RequestParam注解来实现默认值,WebConsts是当前项目中包含常量的类
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE)
                    Integer page,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE)
                    Integer pageSize
    ){
        JsonPage<ShopStandardVO> jsonPage=
                shopService.listShops(page,pageSize);
        return JsonResult.ok(jsonPage);
    }

    @PostMapping("/delete")
    @ApiOperation("根据id数组删除购物车中商品信息")
    @ApiImplicitParam(value = "要删除的id数组",name="ids",
            required = true, dataType = "array")
    //@PreAuthorize("hasAuthority('ROLE_user')")
    public JsonResult removeShopsByIds(Long[] ids){
        shopService.removeShop(ids);
        return JsonResult.ok("删除完成!");
    }

    @PostMapping("/delete/all")
    @ApiOperation("清空当前登录用户的购物车商品信息")
    //@PreAuthorize("hasRole('user')")
    public JsonResult removeShopsByUserId(){
        shopService.removeAllShops();
        return JsonResult.ok("购物车清空!");
    }

    @PostMapping("/update/num")
    @ApiOperation("修改购物车中sku商品数量")
    //@PreAuthorize("hasRole('user')")
    public JsonResult updateNum(@Validated ShopUpdateDTO shopUpdateDTO){
        shopService.updateNum(shopUpdateDTO);
        return JsonResult.ok("修改完成");
    }

}
